##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtCore, QtGui
from lisp.layout.qmediawidget import QMediaWidget
from lisp.core.plugin import Plugin
from lisp.core.media import Media
import subprocess


class MediaInfo(Plugin):

    PLUGIN_NAME = 'MediaInfo'

    def __init__(self, main):
        self.mainWidow = main.mainWindow

        self.actionMediaInfo = QtGui.QAction(None, triggered=self.showInfo)
        self.actionMediaInfo.setText("Media Info")
        QMediaWidget.CONTEXT_MENU_ACTIONS.append(QMediaWidget.CONTEXT_MENU_SEPARATOR)
        QMediaWidget.CONTEXT_MENU_ACTIONS.append(self.actionMediaInfo)

    def showInfo(self, clicked):
        if(QMediaWidget.CONTEXT_TEMP_MEDIA.state == Media.NONE):
            QtGui.QMessageBox.critical(None, 'Error Message', 'Empty Media!!')
            return
        soxOut = subprocess.getoutput('soxi "' + QMediaWidget.CONTEXT_TEMP_MEDIA.conf['location'] + '"')[1:-1].split('\n')
        for par in soxOut:
            pos1 = par.find(':')
            pos2 = par.find('=')
            if(pos1 >= 0 and (pos1 < pos2 or pos2 < 0)):
                name = par[:pos1]
                data = par[pos1 + 1:]
            elif(pos2 >= 0):
                name = par[:pos2]
                data = par[pos2 + 1:]
            else:
                name = 'Error'
                data = 'Syntax not supported'
            soxOut[soxOut.index(par)] = [name.strip(), data.strip()]
        infoD = InfoDialog(self.mainWidow, soxOut, QMediaWidget.CONTEXT_TEMP_MEDIA_CONF['name'])
        infoD.exec_()


class InfoDialog(QtGui.QDialog):

    def __init__(self, parent, info, trackName):
        super(InfoDialog, self).__init__(parent)

        self.setWindowTitle('Media Info - ' + trackName)
        self.setWindowModality(QtCore.Qt.ApplicationModal)
        self.setMinimumSize(450, 250)
        self.resize(450, 250)

        self.VLayout = QtGui.QVBoxLayout(self)

        self.infoT = QtGui.QTableWidget(len(info), 2, self)
        self.infoT.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
        self.infoT.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.infoT.setHorizontalHeaderLabels(['Scope', 'Value'])
        self.infoT.setGeometry(5, 5, 440, 200)
        for n in range(len(info)):
            self.infoT.setItem(n, 0, QtGui.QTableWidgetItem(info[n][0]))
            self.infoT.setItem(n, 1, QtGui.QTableWidgetItem(info[n][1]))
        self.infoT.resizeColumnsToContents()

        self.VLayout.addWidget(self.infoT)

        self.buttonBox = QtGui.QDialogButtonBox(self)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Close)
        self.buttonBox.move(340, 215)

        self.VLayout.addWidget(self.buttonBox)

        self.buttonBox.rejected.connect(self.close)
